package com.hqyj.second.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.second.domain.Users;
import com.hqyj.second.json.R;
import com.hqyj.second.service.UsersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("users")
@RestController //@Controller +@ResponseBody
public class UsersController {

    @Resource
    private UsersService usersService;

    /*
    @GetMapping("auth")
    public Map<String, Object> login(@RequestParam("username") String username, @RequestParam("password") String password) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Users loginUser = usersService.login(username, password);
            if (loginUser != null) {
                result.put("code", 200);
                result.put("msg", "登录成功！");
                result.put("data", loginUser);

            } else {
                result.put("code", 400);
                result.put("msg", "登录失败！请检查用户名或者密码是否正确");
            }
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("code", 500);
            result.put("msg", "程序出现异常！");
            return result;
        }
    }*/

    @GetMapping("auth")
    public R login(@RequestParam("username") String username, @RequestParam("password") String password) {
        //Map<String, Object> result = new HashMap<String, Object>();
        try {
            Users loginUser = usersService.login(username, password);
            if (loginUser != null) {
                return R.success("登录成功！", loginUser);
            }
            return R.fail("登录失败，请检查用户名或者密码是否正确！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    //新增用户和注册用户一回事，没有任何区别。
    //更新了reg接口
    @PostMapping("/")
    public R reg(@RequestBody Users u) {
        try {
            if (usersService.insertUser(u)) {
                return R.success("恭喜您，注册成功！");
            }
            return R.fail("注册失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @PutMapping("/")
    public R update(@RequestBody Users u) {
        try {
            if (usersService.modifyUser(u)) {
                return R.success("修改成功！");
            }
            return R.fail("修改失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }


    @GetMapping("/")
    public R queryUserByUid(@RequestParam("uid") Integer uid) {
        try {
            Users u = usersService.queryUserByUid(uid);
            if (u != null) {
                return R.success("查询用户资料成功！", u);
            }
            return R.fail("该用户不存在！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }


    @DeleteMapping("/")
    public R removeUser(@RequestParam("uid") Integer uid) {
        try {
            if (usersService.removeUser(uid)) {
                return R.success("删除成功！");
            }
            return R.fail("删除失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常");
        }
    }


    @GetMapping("/all")
    public R queryAllUsers() {
        try {
            List<Users> usersList = usersService.queryAllUsers();
            return R.success("查询用户列表成功！", usersList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/pager")
    public R queryUsersPagerList(@RequestParam("currentPagerNumber") Integer currentPagerNumber) {
        try {
            Page<Users> page = usersService.queryUsersPager(currentPagerNumber);
            return R.success("查询用户分页数据成功！", page);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @DeleteMapping("/batch")
    public R removeBatchUsers(@RequestBody List<Integer> ids) {
        try {
            usersService.removeBatchUsers(ids);
            return R.success("批量删除成功！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

}
